using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace hemsidan.change
{
	/// <summary>
	/// Summary description for WriteBlog.
	/// </summary>
	public partial class WriteBlog : Bases.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (!IsPostBack)
			{
				if (Request.QueryString["BlogId"] != null)
				{
					this.BlogId = int.Parse(Request.QueryString["BlogId"]);
					DataTable blogItem = Entities.Blog.GetBlog(this.BlogId);
					txtHeader.Text = (string)blogItem.Rows[0]["Header"];
					this.Text = (string)blogItem.Rows[0]["TextField"];
				}
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void btnSave_Click(object sender, System.EventArgs e)
		{
			if (BlogId == 0)
			{
				Entities.Blog.CreateBlog(this.txtHeader.Text, this.Text);
			}
			else
			{
				Entities.Blog.UpdateBlog(this.BlogId, this.txtHeader.Text, this.Text);
				this.BlogId = 0;
			}
			Response.Redirect(Helpers.Settings.VirtualRoot + "blog.aspx");
		}

		protected void btnDelete_Click(object sender, System.EventArgs e)
		{
			Entities.Blog.DeleteBlog(this.BlogId);
			this.Text = string.Empty;
			this.txtHeader.Text = string.Empty;
			this.BlogId = 0;
		}

		private int BlogId
		{
			get 
			{
				if (this.Session["BlogId"] == null)
					  return 0;
				  else
					  return (int)this.Session["BlogId"];
			}
			set 
			{
				this.Session["BlogId"] = value; 
				btnDelete.Visible = (value > 0);
			}
		}

		private string Text
		{
			get { return this.txtText.Text; }
			set { this.txtText.Text = value.Replace("<br>","\n");}
		}
	}
}
